package main

import "fmt"

//#link: https://leetcode-cn.com/problems/find-peak-element/
func main() {
	nums := []int{1, 2, 3, 1}
	fmt.Println(findPeakElement(nums))
}

func findPeakElement(nums []int) int {
	if len(nums) == 2 {
		if nums[0] > nums[1] {
			return 0
		} else {
			return 1
		}
	}
	if len(nums) > 2 && nums[len(nums)-1] > nums[len(nums)-2] {
		return len(nums) - 1
	}
	mid := 1
	for mid < len(nums)-1 {
		left, right := mid-1, mid+1
		if nums[mid] > nums[left] && nums[mid] > nums[right] {
			return mid
		}
		mid++
	}
	return 0
}
